package com.im.service.tenderInfo.smo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.im.service.dto.*;
import com.im.service.orgarization.dao.IOrgarizationQueryDao;
import com.im.service.orgarization.smo.IOrgarizationQuerySmo;
import com.im.service.user.dao.IStaffQueryDAO;
import com.im.viewmodel.projectInvitManage.EvalBidPartyVo;
import com.im.viewmodel.projectInvitManage.ResultVo;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.im.service.bidInfo.smo.IBidInfoQuerySMO;
import com.im.service.docManage.dao.IDocManageQueryDAO;
import com.im.service.evalBidParty.smo.IEvalBidPartyQuerySMO;
import com.im.service.model.ContractItemVo;
import com.im.service.projectInfo.dao.IImplementProjQueryDAO;
import com.im.service.purchasePlan.dao.IPurchasePlanQueryDAO;
import com.im.service.result.smo.IResultQuerySMO;
import com.im.service.tenderInfo.dao.ITenderInfoQueryDAO;
import com.im.util.ListUtil;
import com.im.viewmodel.projectInvitManage.InvitInfoManageReqVo;
import com.im.viewmodel.projectInvitManage.InvitInfoManageRspVo;

@Service
public class TenderInfoQuerySMOImpl implements ITenderInfoQuerySMO{


	@Autowired
	private ITenderInfoQueryDAO tenderInfoQueryDAO;
	@Autowired
	private IDocManageQueryDAO docManageQueryDAO;
	@Autowired
	private IPurchasePlanQueryDAO purchasePlanQueryDAO;
	@Autowired
	private IImplementProjQueryDAO implementProjQueryDAO;
	@Autowired
	private IBidInfoQuerySMO bidInfoQuerySMO;
	@Autowired
	private IResultQuerySMO resultQuerySMO;
	@Autowired
	private IEvalBidPartyQuerySMO evalBidPartyQuerySMO;
	@Autowired
	private IOrgarizationQuerySmo orgarizationQuerySmo;
	@Autowired
	private IStaffQueryDAO staffQueryDAO;

	@Override
	public PageQuery<InvitInfoManageRspVo> queryInvitInfo(InvitInfoManageReqVo reqVo, SearchOption searchOption) throws Exception {

		List<InvitInfoManageRspVo> listVo = new ArrayList<InvitInfoManageRspVo>();
		Map<String,Object> inMap = new HashMap<String,Object>();
		// 根据  实施项目id 项目名称  文件名称
		String planProjClass = reqVo.getPlanProjClass();
		if(!StringUtil.isEmpty(planProjClass)){
			inMap.put("planProjClass", planProjClass);
		}
		String projectId = reqVo.getProjectId();
		if(!StringUtil.isEmpty(projectId)){
			inMap.put("impleProjId", projectId);
		}
		String developOrg = reqVo.getOrgId();
		if(!StringUtil.isEmpty(developOrg)){
			inMap.put("developOrg", developOrg);
		}

		String statusCd = reqVo.getStatusCd();
		if(!StringUtil.isEmpty(statusCd)){
			inMap.put("statusCd", statusCd);
		}
		PurchasePlan pcp = new PurchasePlan();
		List<Long> statusCds = new ArrayList<Long>();
//        statusCds.add(10L);
//        statusCds.add(30L);
        Result ret = new Result();
//        inMap.put("statusCds",statusCds);
		PageQuery<TenderInfo> pageList = queryTenderInfoForPage(inMap,searchOption);
		List<TenderInfo> tends = pageList.getRows();
		for(TenderInfo ti : tends){
			InvitInfoManageRspVo rspVo = new InvitInfoManageRspVo();
			rspVo.setTenderInfo(ti);
			//  查询 实施项目
			inMap.clear();
			ImplementProject impleProj = new ImplementProject();
			impleProj.setImpleProjId(ti.getImpleProjId());
			inMap.put("impleProj", impleProj);
			ImplementProject project = implementProjQueryDAO.queryImpleProjDetail(inMap);
			if(project == null){
				throw new BaseException("查询实施工程信息为空");
			}

            Orgarization reqObj = new Orgarization();
            reqObj.setOrgId(project.getDevelopOrg());
            Orgarization orgarization = orgarizationQuerySmo.queryOrgarizationDetail(reqObj);
			project.setDevelopOrgName(orgarization.getOrgName());
			rspVo.setImplementProject(project);

			if(!StringUtil.isEmpty(ti.getDocManageId())){
				//  查询文件管理信息
				DocManage docManage = docManageQueryDAO.queryDocManageById(ti.getDocManageId());
				if(docManage == null){
					throw new BaseException("查询文件管理信息为空");
				}
				rspVo.setDocManage(docManage);
			}


			// 通过项目id 查询 采购计划
			List<String> impleProjIds = new ArrayList<String>();
			impleProjIds.add(ti.getImpleProjId());
			PurchasePlan purchasePlan = new PurchasePlan();
			purchasePlan.setPurchPlanId(ti.getPurchPlanId());
			inMap.clear();
			inMap.put("impleProjIds", impleProjIds);
			inMap.put("purchasePlan", purchasePlan);
			List<PurchasePlan> purList = purchasePlanQueryDAO.queryPurchasePlanList(inMap);
			if(!ListUtil.isListEmpty(purList)){
				pcp = purList.get(0);
				rspVo.setPurchasePlan(pcp);
			}

			// 根据  投标信息id投标信息
			inMap.clear();
			inMap.put("tenderInfoId", ti.getTenderInfoId());
			List<BidInfo> bidList = bidInfoQuerySMO.queryBidInfoList(inMap);
			if(!ListUtil.isListEmpty(bidList)){
                BidInfo bidInfo = bidList.get(0);
                rspVo.setBidInfo(bidInfo);
			}

			// 中标结果
			List<Result> retList = resultQuerySMO.queryResultList(inMap);
    		if(!ListUtil.isListEmpty(retList)){
				ret = retList.get(0);
				ResultVo rv= new ResultVo();
				PropertyUtils.copyProperties(rv,ret);
				if (ret.getOrgId() != null) {
					Orgarization org = new Orgarization();
					org.setOrgId(ret.getOrgId());
					org = orgarizationQuerySmo.queryOrgarizationDetail(org);
					if(org!=null && !StringUtil.isEmpty(org.getOrgName())){
						rv.setOrgName(org.getOrgName());
					}
				}

				rspVo.setResultVo(rv);
				// 评标参与人
				inMap.clear();
				inMap.put("resultId", ret.getResultId());
				List<EvalBidParty> ebpList = evalBidPartyQuerySMO.queryEvalBidPartyList(inMap);
				if(ListUtil.isListEmpty(ebpList)){
					throw new BaseException("查询 评标参与人信息为空");
				}

				List<EvalBidPartyVo> evs = new ArrayList<EvalBidPartyVo>();
				for(EvalBidParty ebp : ebpList){
					EvalBidPartyVo evalBidPartyVo = new EvalBidPartyVo();
					PropertyUtils.copyProperties(evalBidPartyVo,ebp);
					inMap.clear();
					inMap.put("staffId",ebp.getReviewer());
					Staff staff = staffQueryDAO.queryStaff(inMap);
					if(staff != null){
						evalBidPartyVo.setReviewerName(staff.getName());
					}
					evs.add(evalBidPartyVo);
				}
				rspVo.setEvalBidPartyVos(evs);
			}

			listVo.add(rspVo);
		}

		//Page<InvitInfoManageRspVo> data=(Page<InvitInfoManageRspVo>)listVo;
		PageQuery<InvitInfoManageRspVo> page=new PageQuery<InvitInfoManageRspVo>();
		page.setRows(listVo);
		page.setTotal(pageList.getTotal());
		return page;

	}

	@Override
	public TenderInfo queryTenderInfoById(String id) {
		return tenderInfoQueryDAO.queryTenderInfoById(id);
	}

	@Override
	public PageQuery<TenderInfo> queryTenderInfoForPage(Map<String, Object> inMap, SearchOption searchOption) throws Exception {

		PageHelper.startPage(searchOption.getPageNumber(), searchOption.getPageSize());

		inMap.put("searchOption",searchOption);
		List<TenderInfo> list = tenderInfoQueryDAO.queryTenderInfoForPage(inMap);

		Page<TenderInfo> data=(Page<TenderInfo>)list;
		PageQuery<TenderInfo> page=new PageQuery<TenderInfo>();
		page.setRows(list);
		page.setTotal(data.getTotal());
		return page;
	}

    @Override
    public List<TenderInfo> queryTenderInfoList(Map<String,Object> inMap) throws Exception{
        List<TenderInfo> list = tenderInfoQueryDAO.queryTenderInfoForPage(inMap);
        return list;
    }

	public List<TenderInfo> querySuccTenderInfoList(Map<String,Object> inMap) throws Exception{
		List<TenderInfo> list = tenderInfoQueryDAO.queryTenderInfoList(inMap);
		return list;
	}

	@Override
	public PageQuery<TenderInfo> queryTenderInfoListPage(Map<String, Object> inMap) throws Exception {
		List<TenderInfo> list = tenderInfoQueryDAO.queryTenderInfoList(inMap);

		Page<TenderInfo> data=(Page<TenderInfo>)list;
		PageQuery<TenderInfo> page=new PageQuery<TenderInfo>();
		page.setRows(list);
		page.setTotal(data.getTotal());
		return page;
	}

	@Override
	public List<TenderInfo> queryTenderInfoListForNotice(Map<String,Object> inMap) throws Exception{
		List<TenderInfo> list = tenderInfoQueryDAO.queryTenderInfoListForNotice(inMap);
		return list;
	}

	@Override
	public List<TenderInfo> querySuccTendInfoListForOpenBid(Map<String,Object> inMap) throws Exception{
		List<TenderInfo> list = tenderInfoQueryDAO.querySuccTendInfoListForOpenBid(inMap);
		return list;
	}

	@Override
	public List<TenderInfo> queryTenderInfoForNoticeByNoticeId(Map<String,Object> inMap) throws Exception{
		List<TenderInfo> list = tenderInfoQueryDAO.queryTenderInfoForNoticeByNoticeId(inMap);
		return list;
	}

}
